package com.codingke.codingbookstore.ui;

import android.os.Bundle;

import com.codingke.codingbookstore.R;
import com.codingke.codingbookstore.adapter.CommentAdapter;
import com.codingke.codingbookstore.vo.Comment;
import com.handmark.pulltorefresh.library.ILoadingLayout;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.listener.FindListener;

public class CommentActivity extends BaseActivity implements PullToRefreshBase.OnRefreshListener2{

    private PullToRefreshListView pull_to_refresh_listview;
    private CommentAdapter commentAdapter;
    private ArrayList<Comment> comments = new ArrayList<>();
    private String bookInfoId;
    private int limit = 20;
    private int currentCount = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_comment);

        bookInfoId = getIntent().getStringExtra("bookInfoId");

        pull_to_refresh_listview = (PullToRefreshListView) findViewById(R.id.pull_to_refresh_listview);
        pull_to_refresh_listview.setOnRefreshListener(this);
        pull_to_refresh_listview.setMode(PullToRefreshBase.Mode.BOTH);
        //设置刷新时显示的文本
        ILoadingLayout startLayout = pull_to_refresh_listview.getLoadingLayoutProxy(true, false);
        startLayout.setPullLabel("正在下拉刷新");
        startLayout.setRefreshingLabel("正在努力加载中");
        startLayout.setReleaseLabel("放开刷新");

        ILoadingLayout endLayout = pull_to_refresh_listview.getLoadingLayoutProxy(false, true);
        endLayout.setPullLabel("正在上拉刷新");
        endLayout.setRefreshingLabel("正在努力加载中");
        endLayout.setReleaseLabel("放开刷新");

        initData();

    }

    private void initData() {
        commentAdapter = new CommentAdapter(this,comments);
        pull_to_refresh_listview.setAdapter(commentAdapter);
        loadComment();
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase refreshView) {
        loadComment();
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase refreshView) {
        loadComment();
    }


    private void loadComment(){
        BmobQuery<Comment> query = new BmobQuery<>();
        query.addWhereEqualTo("bookInfoId",bookInfoId);
        query.order("-createdAt");
        query.setLimit(limit);
        query.setSkip(currentCount); //跳过多少条记录进行查询
        query.findObjects(this, new FindListener<Comment>() {
            @Override
            public void onSuccess(List<Comment> list) {
                for (int i=0;i<list.size();i++){
                    comments.add(list.get(i));
                }
                currentCount+=list.size();
                commentAdapter.notifyDataSetChanged();
                pull_to_refresh_listview.onRefreshComplete();
            }

            @Override
            public void onError(int i, String s) {

            }
        });
    }
}
